package com.example.demo.controller;

import com.alibaba.fastjson.JSON;
import com.example.demo.common.Result;
import com.example.demo.pojo.Record;
import com.example.demo.pojo.User;
import com.example.demo.service.AdminService;
import com.example.demo.service.impl.AdminServiceImpl;
import com.example.demo.utils.AbsentUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class AdminController {
    @Autowired
    private AdminService adminService;

    @RequestMapping("/admin/selectAllRecord")
    public Result selectAllRecord() {
        try {
            List<Record> recordList= adminService.selectAllRecord();
            if (recordList != null || recordList.size() != 0){
                return Result.success(recordList);
            }
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/admin/selectAbsentUser")
    public Result selectAbsentUser() {
        try {
            List<AbsentUser> absentUserList= adminService.selectAbsentUser();
            if (absentUserList != null || absentUserList.size() != 0){
                return Result.success(absentUserList);
            }
            else return Result.fail();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/admin/makeUserOn")
    public Result makeUserOn(@RequestParam("Uid") int Uid) {
        try {
            int i = adminService.makeUserOn(Uid);
            return Result.success(i);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/admin/makeUserOff")
    public Result makeUserOff(@RequestParam("Uid") int Uid) {
        try {
            int i = adminService.makeUserOff(Uid);
            return Result.success(i);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/admin/AdminLogin")
    public Result AdminLogin(@RequestParam("Aid") String Aid) {
        try {
            String password = adminService.AdminLogin(Aid);
            return Result.success(password);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/admin/selectAllUser")
    public Result selectAllUser() {
        try {
            List<User> userList=adminService.selectAllUser();
            return Result.success(userList);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/admin/addUser")
    public Result addUser(@RequestBody String userStr) {
        try {
            System.out.println(userStr);
            User user = JSON.parseObject(userStr, User.class);
            System.out.println(user);
            int i = adminService.addUser(user);
            return Result.success(i);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }

    @RequestMapping("/admin/updateUser")
    public Result updateUser(@RequestBody String userStr) {
        try {
            User User = JSON.parseObject(userStr, User.class);
            int i = adminService.updateUser(User);
            return Result.success(i);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail();
        }
    }
    /*List<Record> selectAllRecord();
    List<AbsentUser> selectAbsentUser();
    int addUser(User user);
    int makeUserOn(String Uid);
    int makeUserOff(String Uid);
    int updateUser(User user);*/

}
